package com.enqualcomm.kids.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.enqualcomm.kids.R;

import common.utils.DensityUtil;
import common.utils.MyFrontUtil;

/**
 * 确认对话框
 *
 * @author yzq
 */
public class MyCommitDialog extends Dialog {
    private CommitDialogCallBack callBack;
    private SpannableStringBuilder msg1;
    private String msg2;
    private String title;
    private String sureBtnName;
    private String cancelBtnName;
    private String watchName;
    private Context context;

    /**
     * @param context
     * @param title
     * @param msg
     * @param callBack
     */
    public MyCommitDialog(Context context, String title, SpannableStringBuilder msg, CommitDialogCallBack callBack) {
        super(context);
        this.callBack = callBack;
        this.title = title;
        this.msg1 = msg;
    }

    public MyCommitDialog(Context context, String title, String msg, CommitDialogCallBack callBack) {
        super(context);
        this.callBack = callBack;
        this.title = title;
        this.msg2 = msg;
        this.context = context;
    }

    public MyCommitDialog(Context context, String title, String msg, String sureBtnName, CommitDialogCallBack callBack) {
        super(context);
        this.callBack = callBack;
        this.title = title;
        this.msg2 = msg;
        this.sureBtnName = sureBtnName;
        this.context = context;
    }

    public MyCommitDialog(Context context, String title, String msg, String sureBtnName, String cancelBtnName, CommitDialogCallBack callBack) {
        super(context);
        this.callBack = callBack;
        this.title = title;
        this.msg2 = msg;
        this.sureBtnName = sureBtnName;
        this.cancelBtnName = cancelBtnName;
        this.context = context;
    }

    public MyCommitDialog(Context context, String title, String msg, String watchName, String sureBtnName, String cancelBtnName, CommitDialogCallBack callBack) {
        super(context);
        this.callBack = callBack;
        this.title = title;
        this.msg2 = msg;
        this.sureBtnName = sureBtnName;
        this.cancelBtnName = cancelBtnName;
        this.watchName = watchName;
        this.context = context;
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.setContentView(R.layout.dialog_cancelterminal);

        setDialogAttributes();
        initDialogView();
    }

    public void setDialogAttributes() {
        Window dialogWindow = getWindow();
        setCanceledOnTouchOutside(false);
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();

        DisplayMetrics dm = new DisplayMetrics();
        dialogWindow.getWindowManager().getDefaultDisplay().getMetrics(dm);

        lp.width = dm.widthPixels - DensityUtil.dip2px(getContext(), 30); // 宽度
        // lp.alpha = 0.9f; // 透明度

        dialogWindow.setAttributes(lp);
        dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    public void initDialogView() {
        TextView myTerminal = (TextView) findViewById(R.id.myTerminal);
        myTerminal.setText(title);
        TextView dialog_msg_tv = (TextView) findViewById(R.id.dialog_msg_tv);
        if (msg1 != null) {
            dialog_msg_tv.setText(msg1);
        } else {
            dialog_msg_tv.setText(msg2);
        }

        Button sureBtn = (Button) findViewById(R.id.sureBtn);
        sureBtn.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View arg0, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    callBack.onCommit();
                    dismiss();
                }
                return false;
            }
        });
        if (!TextUtils.isEmpty(sureBtnName)) {
            sureBtn.setText(sureBtnName);
        }

        Button cancelBtn = (Button) findViewById(R.id.cancelBtn);

        cancelBtn.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View arg0, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    callBack.onCancel();
                    dismiss();
                }
                return false;
            }
        });

        if (!TextUtils.isEmpty(cancelBtnName)) {
            cancelBtn.setText(cancelBtnName);
        }
        if (!TextUtils.isEmpty(watchName) && title.equals(context.getString(R.string.power_off))) {
            MyFrontUtil.initTextViewText(context, myTerminal, watchName, context.getString(R.string.t_m_off_begin),
                    context.getString(R.string.t_m_off_end), 2, watchName.length() + 2);
        }

        if (!TextUtils.isEmpty(watchName) && title.equals(context.getString(R.string.search_terminal))) {
            MyFrontUtil.initTextViewText(context, myTerminal, watchName, context.getString(R.string.t_m_track_begin),
                    context.getString(R.string.t_m_track_end), 2, watchName.length() + 2);
        }
        if (!TextUtils.isEmpty(watchName) && title.equals(context.getString(R.string.manager_power_reset))) {
            MyFrontUtil.initTextViewText(context, myTerminal, watchName, context.getString(R.string.t_m_reboot_begin),
                    context.getString(R.string.t_m_reboot_end), 2, watchName.length() + 2);
        }
    }

    /**
     * 隐藏取消按钮
     */
    public void hideCancelBtn() {
        findViewById(R.id.cancelBtn).setVisibility(View.GONE);
    }
}
